//
//  CAISegmentedControl.h
//  Demo
//
//  Created by simon on 2017/9/5.
//  Copyright © 2017年 simon. All rights reserved.
//
//  注释：用UICollectionView自定义一个ZXSegmentControl；底部加选中的红色指示条；
//  2018.3.6,新增选择条样式；新增segment宽度样式；
//  2018.5.07  新增注释

#import <UIKit/UIKit.h>


NS_ASSUME_NONNULL_BEGIN
/// 文本延伸宽度
static CGFloat const ZXSegmentedTitleExtendWidth = 10.f;

/// 选中指示条样式
typedef NS_ENUM(NSInteger, CAISegmentedControlSelectionStyle) {
    
    ///  line:整个cell宽度；
    CAISegmentedControlSelectionStyleFullWidthStripe = 0,
    
    ///  line:选择条是文字对应宽度；
    CAISegmentedControlSelectionStyleTextWidthStripe = 1,
    
    ///  line:title宽度+ZXSegmentedTitleExtendWidth
    CAISegmentedControlSelectionStyleExtendTextWidthStripe = 2,

    ///  圆弧：圆弧图标
    CAISegmentedControlSelectionStyleCircular = 3,

};

/// 设置每个segment的宽度样式；
typedef NS_ENUM(NSInteger, CAISegmentedControlSegmentItemWidthStyle)
{
    /// 固定宽度，根据view的宽度得出每个segment的平均宽度；适用于固定宽度的；
    CAISegmentedControlSegmentItemWidthStyleFixed = 0,   // Segment width is fixed
    /// 适用于超过一个屏幕的
    CAISegmentedControlSegmentItemWidthStyleDynamic = 1, // Segment width will only be as big as the text width (including inset)
};



@protocol CAISegmentedControlDelegate,CAISegmentedControlDataSource;
@class CAISegmentCell;
@interface CAISegmentedControl : UIView


@property (nonatomic, weak) id<CAISegmentedControlDelegate>delegate;
@property (nonatomic, weak) id<CAISegmentedControlDataSource>dataSource;

@property (nonatomic, strong) UICollectionView *collectionView;

/// 最小item之间间距；default 10
@property (nonatomic, assign) CGFloat minimumItemSpacing;

/// 外边距:Default is UIEdgeInsetsMake(0, 8, 0, 8)
@property (nonatomic, assign) UIEdgeInsets segmentEdgeInset;


/// segment宽度样式,默认CAISegmentedControlSegmentItemWidthStyleDynamic
@property (nonatomic, assign) CAISegmentedControlSegmentItemWidthStyle widthStyle;


/// 文字默认颜色
@property (nonatomic, strong) UIColor *normalColor;
/// 文字选中颜色
@property (nonatomic, strong) UIColor *selectedColor;

/// 文字默认字体大小
@property (nonatomic, strong) UIFont *font;
/// 文字选中字体大小
@property (nonatomic, strong) UIFont *selectedFont;


/// 选中指示条

/// 选中指示条样式,默认CAISegmentedControlSelectionStyleFullWidthStripe；
@property (nonatomic, assign) CAISegmentedControlSelectionStyle selectionStyle;

/// 设置选中指示条颜色
@property (nonatomic, strong) UIColor *selectionIndicatorColor;

/// 设置选中指示条高度， Default is 3.0,CAISegmentedControlSelectionStyleCircular不受此影响
@property (nonatomic, readwrite) CGFloat selectionIndicatorHeight;

/// 设置选中指示条外边距，默认UIEdgeInsetsMake(0, 0, 1, 0)
@property (nonatomic, assign) UIEdgeInsets selectionIndicatorMargin;



/// BottomDivide

/// 是否隐藏segment的底部margin线，默认NO；
@property (nonatomic, assign) BOOL hideBottomDivideLine;
@property (nonatomic, strong) UIColor *bottomDivideLineColor;


@property (nonatomic, assign) NSInteger selectedIndex;

/// 文字数组
@property (nonatomic, strong) NSArray *sectionTitles;

+ (instancetype)segmentControlWithSectionTitles:(NSArray<NSString *> *)titles;



- (void)setSelectedIndex:(NSInteger)selectedIndex animation:(BOOL)animation;

- (void)reloadData;

@end


@protocol CAISegmentedControlDelegate <NSObject>

/// 设置文本显示外观
/// - Parameters:
///   - segmentedControl: segmentedControl description
///   - cell: cell description
///   - indexPath: indexPath description
- (void)segmentControl:(CAISegmentedControl *)segmentedControl
       willDisplayCell:(CAISegmentCell *)cell
    forItemAtIndexPath:(NSIndexPath *)indexPath;


/// 选中回调
/// - Parameters:
///   - segmentedControl: segmentedControl description
///   - index: index description
- (void)segmentControl:(CAISegmentedControl *)segmentedControl
      didSelectedIndex:(NSInteger)index;

@end



@protocol CAISegmentedControlDataSource <NSObject>

@required

//- (NSAttributedString *)CAISegmentedControl:(CAISegmentedControl *)CAISegmentedControl attributedTitleForSelectItemAtIndexPath:(NSIndexPath *)indexPath;
//
//- (NSAttributedString *)CAISegmentedControl:(CAISegmentedControl *)CAISegmentedControl attributedTitleForUnSelectItemAtIndexPath:(NSInteger)index;

@end

//一定要写在viewWillLayoutSubviews设置frame
/*
 - (void)viewWillLayoutSubviews {
  [super viewWillLayoutSubviews];
 
  UIViewController *vc = [self.dataSource objectAtIndex:self.selectedIndex];
  [self.pageViewController setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
  self.segmentControl.frame = CGRectMake(0, 0, self.view.frame.size.width, self.segmentHeight);
 
 }
 */






@interface ZXSegmentModel : NSObject

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSAttributedString *attributedTitle;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) BOOL selected;

@property (nonatomic, strong) UIFont *font;

@property (nonatomic, strong) UIFont *selectedFont;

@end


NS_ASSUME_NONNULL_END





